package com.zhiyume.service;

import com.zhiyume.dao.api.TDramaPersonMapper;
import com.zhiyume.dao.entity.TDramaPerson;
import com.zhiyume.dao.entity.TDramaPersonCriteria;
import com.zhiyume.dao.entity.TDramaWithBLOBs;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.service.util.GenerateKeyUtil;
import com.zhiyume.util.TypeConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 演员剧关联相关
 */
@Service
public class DramaActorService {
    @Autowired
    TDramaPersonMapper tDramaPersonMapper;

    public void addDramaPerson(TDramaWithBLOBs tDrama){

        String[] actorNameArr = tDrama.getActor().split(",");
        String[] actorIdArr = tDrama.getActorId().split(",");

        TDramaPersonCriteria example = new TDramaPersonCriteria();
        TDramaPersonCriteria.Criteria c = example.createCriteria();
        c.andDramaIdEqualTo(tDrama.getId());
        tDramaPersonMapper.deleteByExample(example);

        for (int i=0,len=actorNameArr.length;i<len;i++){
            TDramaPerson tDramaPerson = new TDramaPerson();
            tDramaPerson.settId(GenerateKeyUtil.generationPK());
            tDramaPerson.setCreateTime(DateUtil.getNow());
            tDramaPerson.setUpdateTime(DateUtil.getNow());
            tDramaPerson.setpId(Long.valueOf(actorIdArr[i]));
            tDramaPerson.setpName(actorNameArr[i]);
            tDramaPerson.setcId(TypeConstants.PersonConstant.PERSON_ACTOR);
            tDramaPerson.setcName((String) TypeConstants.carrerMap.get(TypeConstants.PersonConstant.PERSON_ACTOR));
            tDramaPerson.setDramaId(tDrama.getId());
            tDramaPerson.setDramaName(tDrama.getName());
            tDramaPersonMapper.insert(tDramaPerson);
        }
    }
}
